package com.xteam.youin_supplier.fragment;


import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.JsonSyntaxException;
import com.xteam.youin_supplier.MainActivity;
import com.xteam.youin_supplier.R;
import com.xteam.youin_supplier.config.RequestAddr;
import com.xteam.youin_supplier.model.BaseModel;
import com.xteam.youin_supplier.model.Station;
import com.xteam.youin_supplier.util.http.AjaxCallBack;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.FragmentArg;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.ViewsById;
import org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy;

import java.util.List;

/**
 * 加油点详情
 */
@EFragment(R.layout.fragment_station_detail)
public class StationDetailFragment extends BaseFragment {

    @ViewById(R.id.back_btn)
    ImageButton backBtn;

    @ViewById(R.id.toptitle_title)
    TextView topTitle;

    @FragmentArg("time")
    String time;
    @FragmentArg("gasstation_id")
    int gasstation_id;

    @ViewsById({R.id.addr_content, R.id.connect_type_content, R.id.today_add_total_content,
    R.id.today_total_money_content, R.id.in_oil_num_content, R.id.actual_inventory_content,
    R.id.day_profit_loss_content, R.id.month_profit_loss_content})
    List<TextView> detailList;


    @AfterViews
    @Override
    void initView() {
        backBtn.setVisibility(View.VISIBLE);
        topTitle.setText(getResources().getString(R.string.station_detail_title));

        loadData();
    }

    private void loadData() {
        requestParams = "gasstation_id=" + gasstation_id
                + "&time=" + (Integer.parseInt(time) + 1);
        requestUrl = RequestAddr.stationDetail + requestParams;
        xtHttp.get(requestUrl, new AjaxCallBack<Object>() {
            @Override
            public void onSuccess(Object object) {
                super.onSuccess(object);
                dealData(object);
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
            }
        });
    }
    Station station;
    private void dealData(Object object) {
        try {
            station = gson.fromJson(object.toString(), Station.class);
        } catch (JsonSyntaxException e) {
            baseModel = gson.fromJson(object.toString(), BaseModel.class);
            Toast.makeText(getActivity(), baseModel.getMsg(), Toast.LENGTH_SHORT).show();
            return;
        }

        detailList.get(0).setText(station.getData().getLocation());
        detailList.get(1).setText(station.getData().getMobile());
        detailList.get(2).setText(station.getData().getPrice());
        detailList.get(3).setText(station.getData().getQty());
        detailList.get(4).setText(station.getData().getAddamount());
        detailList.get(5).setText(station.getData().getRealamount());
        detailList.get(6).setText("");
        detailList.get(7).setText("");
    }

    @Click({R.id.back_btn})
    @Override
    void Click(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                MainActivity.getInstance().getSupportFragmentManager().popBackStack();
                break;
        }
    }
}
